package blue.qingyu.whimurmur.category;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;

import com.aliyun.oss.model.LiveChannelStat.VideoStat;
import com.jfinal.core.ActionKey;
import com.jfinal.kit.Ret;

import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.JPressConsts;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.core.template.Template;
import io.jpress.core.template.TemplateManager;
import io.jpress.web.base.AdminControllerBase;


@RequestMapping(value = "/admin/addon/category" ,viewPath = "/")
public class CategoryController extends AdminControllerBase {
	private static final List<String> resources = Arrays.asList(new String[] {"page_category.html", "plugin-category"});
	
	@ActionKey("/admin/addon/category")
    @AdminMenu(groupId = JPressConsts.SYSTEM_MENU_ADDON, text = "分类插件")
    public void index() {
    	render("setting.html");
//        setAttr("version","1.0.0");
//        renderText("index");
    }

    public void json() {
        renderJson(Ret.ok().set("message", "json ok...."));
    }

    public void checkPlugin() {
    	Template curTemplate = TemplateManager.me().getCurrentTemplate();
    	File templateDir = curTemplate.getAbsolutePathFile();
    	
    	File[] htmls = templateDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".html"));
    	boolean applied = false;
    	for(File html: htmls) {
    		if(html.getName().equals("page_category.html")) {
    			applied = true;
    			break;
    		}
    	}
    	
    	if(applied) {
    		renderJson(Ret.ok().set("code", 1));
    	}else {
    		renderJson(Ret.ok().set("code", 0));
    	}
    }
    
    public void applyPlugin() {
    	Template curTemplate = TemplateManager.me().getCurrentTemplate();
    	File templateDir = curTemplate.getAbsolutePathFile();
    	File parentFile = templateDir.getParentFile();
    	while(!parentFile.getName().equals("templates")) {
    		parentFile = parentFile.getParentFile();
    	}
    	// 网站根目录
    	parentFile = parentFile.getParentFile();
    	
    	File pluginResource = new File(parentFile.getAbsolutePath() 
    			+ File.separator + "addons" + File.separator + "blue.qingyu.whimurmur.category", "resource");
    	
    	try
		{
    		if(pluginResource.isDirectory()) {
    			for(File resource: pluginResource.listFiles()) {
    	    		if(resource.isDirectory()) {
    	    			File temp = new File(templateDir, resource.getName());
    		    		if(!temp.exists()) {
    						temp.mkdir();
    		    		}
    		    		for(File sub: resource.listFiles()) {
    		    			File temp2 = new File(templateDir.getAbsolutePath() + File.separator 
    		    					+ resource.getName(), sub.getName());
    			    		Files.copy(sub.toPath(), temp2.toPath(), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
    		    		}
    	    		}else {
    	    			File temp = new File(templateDir, resource.getName());
    		    		Files.copy(resource.toPath(), temp.toPath(), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
    	    		}
    	    	}
    		}
    		
    		curTemplate.refresh();
		}
		catch(IOException e)
		{
			e.printStackTrace();
			renderJson(Ret.ok().set("code", 0));
			return;
		}
		
    	renderJson(Ret.ok().set("code", 1));
    }
    
    public void removePlugin() {
    	Template curTemplate = TemplateManager.me().getCurrentTemplate();
    	File templateDir = curTemplate.getAbsolutePathFile();
    	
    	deleteFiles(templateDir, resources);
    	curTemplate.refresh();
    	
    	renderJson(Ret.ok().set("code", 1));
    }
    
    // 从模板中找插件文件并删除
    private void deleteFiles(File dir, List<String> deletes) {
    	boolean deleted = false;
    	for(String del: deletes) {
    		if(dir.getName().startsWith(del)) {
    			deletePlugin(dir);
    			deleted = true;
    			break;
    		}
    	}
    	
    	if(!deleted && dir.isDirectory()) {
    		for(File f: dir.listFiles()) {
    			deleteFiles(f, deletes);
    		}
    	}
    }
    
    // 删除插件文件
    private void deletePlugin(File dir) {
    	if(dir.isDirectory()) {
    		for(File f: dir.listFiles()) {
    			if(f.isFile()) {
    				f.delete();
    			}else {
    				deletePlugin(f);
    			}
    		}
    	}
    	
    	dir.delete();
    }
}
